<?php
error_reporting(0);
$isadmin=1; 
include '../comm/config.php';
include '../comm/msgset.php';
include '../comm/checkpostandget.php';
include 'check.php';

$id=$_POST['id'];
$tradeuser=$_POST['tradeuser'];

if($id=="")
{
    echo "错误：没有要结算的数据！";
    exit;
}

$q = "select * from ".$BIAOTOU."tradelist where Id=".$id;
//echo $q;
$rs=mysql_query($q);
$num = mysql_num_rows($rs);
//echo $num;
if(!$num)
{
	echo "错误：未找到该记录！";
	exit;
}
else
{
	//找出该用户名的ID和等级
	$sql="select Id,dengji from ".$BIAOTOU."user where ddusername='$tradeuser'";
	$rsuser=mysql_query($sql);
	while ($tt = mysql_fetch_array($rsuser)){
		$userid=$tt["Id"]; 
		$dddengji=$tt["dengji"]; 
	}
	
	while ($traderow = mysql_fetch_array($rs)){
		$fxje=ck_fxje($traderow["commission"],$dddengji);
		$tgje=$traderow["commission"]*TGBL; 
		$jyh=$traderow["trade_id"]; 
	}
	
	if($userid!=""){
		
		if(JIFENOPEN==1 && JIFENBL>0){
		    $jifen=round($fxje*JIFENBL);
		    if($jifen>0){
		        //用户消息
			    $msg_tabao_jifen=$jifen."积分！";
		    }
	    }
	    else{
		    $jifen=0;
	    }
		
		$sj= date("Y-m-d H:i:s");  
		//交易列表增加确认人
		$sql="update ".$BIAOTOU."tradelist set outer_code='".$userid."',qrsj='".$sj."',fxje=".$fxje.",checked=2 where id=".$id."";
		mysql_query($sql);
		//用户佣金
		$sql="update ".$BIAOTOU."user set money=money+".$fxje.",jifen=jifen+".$jifen.",dengji=dengji+1 where Id=".$userid."";
		mysql_query($sql);
		//用户消息
		$msg_tabao=str_replace("[trade_id]",$jyh,$msg_tabao);
		$msg_tabao=str_replace("[fxje]",$fxje,$msg_tabao);
		$sql="insert into ".$BIAOTOU."msg (title,content,addtime,see,ddusername,senduser) values ('您获得了新的交易返现！','".$msg_tabao.$msg_tabao_jifen."','$sj',0,'".$tradeuser."','网站客服');";
		//echo $sql;
		mysql_query($sql);
		//用户明细
		$sql="insert into ".$BIAOTOU."mingxi (ddusername,shijian,je,jifen,addtime,memo) values ('".$tradeuser."','交易返现',".$fxje.",".$jifen.",'$sj','交易号".$jyh."')";
		mysql_query($sql);
		//取推荐人
		$sqltjr="select tjr from ".$BIAOTOU."user where ddusername='".$tradeuser."' and tjr<>''";
		$tg=mysql_query($sqltjr);
		while ($tt = mysql_fetch_array($tg)){
			$tjr=$tt["tjr"]; 
		}	
		if($tjr!="")
		{   			
			//推广人佣金
			$sql="update ".$BIAOTOU."user set money=money+".$tgje." where Id='".$tjr."'";
			mysql_query($sql);
			//取推荐人名
			$sqltjr="select ddusername from ".$BIAOTOU."user where Id='".$tjr."'";
			$tgr=mysql_query($sqltjr);
			while ($tu = mysql_fetch_array($tgr)){
				$tjrusername=$tu["ddusername"]; 
			}	
			//推广人明细
			$sql="insert into ".$BIAOTOU."mingxi (ddusername,shijian,je,addtime,memo) values ('".$tjrusername."','推广佣金',".$tgje.",'$sj','交易人".$tradeuser."')";
			mysql_query($sql);
			//推广人消息
			$msg_taobaotuiguang=str_replace("[ddusername]",$tradeuser,$msg_taobaotuiguang);
			$msg_taobaotuiguang=str_replace("[trade_id]",$jyh,$msg_taobaotuiguang);
			$msg_taobaotuiguang=str_replace("[tgje]",$tgje,$msg_taobaotuiguang);
			$sql="insert into ".$BIAOTOU."msg (title,content,addtime,see,ddusername,senduser) values ('您获得了新的推广佣金！','".$msg_taobaotuiguang."','$sj',0,'".$tjrusername."','网站客服')";
			mysql_query($sql);	
		}
	}else
	{
		echo "";
		echo "<script language=javascript>alert('用户".$tradeuser."不存在！！即将跳转到返现设置...');window.location='fanxiantrade.php?id=".$id."'</script>";
		exit;
	}
}
header('Location: checktrade.php');     
mysql_close($link);
?>